package com.ktjy.controller;

import com.ktjy.entity.Information;
import com.ktjy.entity.Page;
import com.ktjy.entity.R;
import com.ktjy.service.InformationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping(value = "/list", produces = "application/json;charset=UTF-8")
@CrossOrigin(origins = {"http://localhost:8085", "http://localhost"}, allowCredentials = "true", maxAge = 3600)
public class InformationController {

        @Autowired
        private InformationService informationService;

        @ResponseBody
        @RequestMapping("/page")
        public Page selectPage (@RequestParam(defaultValue = "1") Integer pageNo,
            @RequestParam(defaultValue = "4") Integer pageSize){
        Information information = new Information();
        return informationService.findPageVo(information, pageNo, pageSize);
    }

        @ResponseBody
        @RequestMapping("/delete/{id}")
        public R<?> delete (@PathVariable Long id){
        try {
            // 调用 service 层删除方法
            informationService.delete(id);
            return R.ok();
        } catch (Exception e) {
            return R.fail("删除失败：" + e.getMessage());
        }
    }
        @ResponseBody
        @PostMapping("/insert")
        public R<?> insert (@RequestBody Information information){
        try {
            // 设置默认值
            Date currentTime = new Date();
            information.setReportTime(currentTime);
            information.setLastMdfTime(currentTime);
            information.setReplyTime(currentTime);  // 添加回复时间
            information.setReplyCount(0L);
            information.setViewCount(0L);

            // 打印调试信息
            System.out.println("插入的数据: " + information);

            int count = informationService.insert(information);
            if (count > 0) {
                return R.ok();
            } else {
                return R.fail("添加失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail("添加失败：" + e.getMessage());
        }
    }

        @ResponseBody
        @GetMapping("/updateContent")
        public R<?> updateContent (@RequestParam Long id){
            try {
                int count = informationService.updateContent(id);
                if (count > 0) {
                    return R.ok();
                } else {
                    return R.fail("更新查看次数失败");
                }
            } catch (Exception e) {
                return R.fail("更新查看次数失败：" + e.getMessage());
            }
        }

        @ResponseBody
        @GetMapping("/updateReplyCount")  // 改为 GetMapping
        public R<?> updateReplyCount(@RequestParam Long id) {  // 使用 @RequestParam 接收参数
            try {
                int count = informationService.updateReplyCount(id);
                if (count > 0) {
                    return R.ok();
                } else {
                    return R.fail("更新回复数失败");
                }
            } catch (Exception e) {
                return R.fail("更新回复数失败：" + e.getMessage());
            }
        }
        @ResponseBody
        @RequestMapping("/selectByRep")
        public List<Information> selectByRep(@RequestParam Long id) {
            try {
                System.out.println("接收到查询请求，ID: " + id); // 添加日志
                List<Information> list = informationService.selectByRep(id);
                System.out.println("查询结果: " + (list != null ? list.toString() : "null")); // 添加日志
                if (list != null) {
                    return list;  // 返回Information对象
                } else {
                    // 如果没有找到数据，返回一个新的空Information对象
                    return null;  // 返回空对象而不是null
                }
            } catch (Exception e) {
                e.printStackTrace();
                // 错误情况下返回一个新的空Information对象
                return null;
            }
        }
}
